package com.hfzy.ihk.common.core.mybatis.typeHandler;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 自定义Boolean类型转换器.
 */
@SuppressWarnings("rawtypes")
public class Boolean2CharTypeHandler implements TypeHandler<Boolean> {

	/** @see TypeHandler#getResult(ResultSet, String)*/
	@Override
	public Boolean getResult(ResultSet rs, String columnName) throws SQLException {
		String str = rs.getString(columnName);
		Boolean rt = Boolean.FALSE;
		if (str.equals("1")){
			rt = Boolean.TRUE;
		}
		return rt;
	}

	/** @see TypeHandler#getResult(ResultSet, int)*/
	@Override
	public Boolean getResult(ResultSet rs, int columnIndex) throws SQLException {

		String str = rs.getString(columnIndex);
		Boolean rt = Boolean.FALSE;
		if (str.equals("1")){
			rt = Boolean.TRUE;
		}
		return rt;
	}

	/** @see TypeHandler#getResult(CallableStatement, int)*/
	@Override public Boolean getResult(CallableStatement cs, int columnIndex) throws SQLException {
		String str = cs.getString(columnIndex);
		Boolean rt = Boolean.FALSE;
		if (str.equals("1")){
			rt = Boolean.TRUE;
		}
		return rt;
	}

	/** @see TypeHandler#setParameter(PreparedStatement, int, Object, JdbcType)*/
	@Override public void setParameter(PreparedStatement arg0, int i, Boolean parameter, JdbcType jdbcType) throws SQLException {
		Boolean b = parameter;
		String value = b == true ? "1" : "0";
		arg0.setString(i, value);
	}

}
